<?php
class MPage extends CI_Model{

	public function __construct()
    {
        parent::__construct();
    }	

	function getPageById($id){
	    $data = array();
	    $this->db->where('page_id',id_clean($id));
	    $this->db->limit(1);
	    $Q = $this->db->get('page');
	    if ($Q->num_rows() > 0){
	      $data = $Q->row_array();
	    }
	
	    $Q->free_result();    
	    return $data;    
	 }
     
     function getNavigationPageByPageId($id)
     {
        $data = array();
        $this->db->select('page_id, parent_id, title, segment');
	    $this->db->where('page_id',id_clean($id));
        $this->db->where('activate',1);
	    $this->db->limit(1);
	    $Q = $this->db->get('page');
	    if ($Q->num_rows() > 0){
	      $data = $Q->row_array();
	    }
	
	    $Q->free_result();    
	    return $data;
     }
	
	function getPagePath($path){
	    $data = array();
	    $this->db->where('segment',db_clean($path));
	    $this->db->where('activate', 1);
	    $this->db->limit(1);
	    $Q = $this->db->get('page');
	    if ($Q->num_rows() > 0){
	      $data = $Q->row_array();
	    }
	
	    $Q->free_result();    
	    return $data;    
	 }
     
     function getPageDropdown()
     {
        $data = array();
		$this->db->order_by("title");
        $Q = $this->db->get('page');
        $data[0] = '...Select Page...';
	    if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[$row['page_id']] = $row['title'];
	       }
         }
         $Q->free_result();    
	     return $data; 	
     }
     
     function getPageEditDropdown($page_id)
     {
        $data = array();
        $this->db->where('page_id !=',id_clean($page_id));
		$this->db->order_by("title");
        $Q = $this->db->get('page');
        $data[0] = '...Select Page...';
	    if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[$row['page_id']] = $row['title'];
	       }
         }
         $Q->free_result();    
	     return $data; 	
     }
		
	 function getAllPages(){
	     $data = array();
         $this->db->distinct();
         $this->db->select("page.page_id, page.title, page.category_id, page.segment, page.activate, category.category");
	     $this->db->from('page');
         $this->db->join('category','category.category_id = page.category_id','left');
	     $this->db->order_by("page.created");
         $Q = $this->db->get();
	     if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[] = $row;
	       }
	    }
	    $Q->free_result();  
	    return $data; 
	 }
     
     function getAllAssignedPages($roleId)
     {
         $data = array();
         $this->db->distinct();
         $this->db->select("page.page_id, page.title, page.category_id, page.segment, page.activate, category.category, role.role_id");
	     $this->db->from('page');
         $this->db->join('category','category.category_id = page.category_id');
         $this->db->join('pagesetting','pagesetting.page_id = page.page_id');
         $this->db->join('role','role.role_id = pagesetting.role_id');
         $this->db->where('role.role_id',id_clean($roleId));
	     $this->db->order_by("page.created");
         $Q = $this->db->get();
	     if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[] = $row;
	       }
	    }
	    $Q->free_result();  
	    return $data;   
             
     }               
     
     function getDropdownAssignedPages()
     {
         $data = array();
         $this->db->distinct();
         $this->db->select("page.page_id, page.title, page.category_id, page.segment, page.activate, category.category");
	     $this->db->from('page');
         $this->db->join('category','category.category_id = page.category_id');
         $this->db->join('pagesetting','pagesetting.page_id = page.page_id');
         $this->db->join('role','role.role_id = pagesetting.role_id');
         $this->db->where('role.role_id',id_clean($_SESSION['role_id']));  
	     $this->db->order_by("page.created");
         $Q = $this->db->get();
	     if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[$row['page_id']] = $row['title'];
	       }
	    }
	    $Q->free_result();  
	    return $data;
     }               
	 
	 function getAllActivePages(){
	     $data = array();
         $this->db->distinct();
         $this->db->select('title, page_id, parent_id, segment');
  	     $this->db->order_by("created");
	     $this->db->where("activate", 1);
         $this->db->where("isshow_in_menu", 1);
	     $this->db->where("parent_id < ", 1);
	     $Q = $this->db->get('page');
	     if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[] = $row;
	       }
	    }
	    $Q->free_result();  
	    return $data; 
	 }
	 
	function getActiveSubPages(){
	     $data = array();
         $this->db->distinct();
         $this->db->select('title, page_id, parent_id, segment');
         $this->db->order_by("created");
	     $this->db->where("activate", 1);
         $this->db->where("isshow_in_menu", 1);
	     $this->db->where('parent_id > ', 0);
	     $Q = $this->db->get('page');
	     if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[] = $row;
	       }
	    }
	    $Q->free_result();  
	    return $data; 
	 }
     
     function search()
     {
         $search = trim($_POST['search']);
         $data = array();
         $this->db->distinct();
         $this->db->select('title, page_id, parent_id, segment, htmlcontent');
         $this->db->order_by("created");
	     $this->db->where("activate", 1);
         $this->db->where("(title LIKE '%".$search."%' OR keyword LIKE '%".$search."%' OR description LIKE '%".$search."%' OR htmlcontent LIKE '%".$search."%')");
	     $Q = $this->db->get('page');
	     if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[] = $row;
	       }
	    }
	    $Q->free_result();  
	    return $data;   
     }
     
     function getRelatedArticles($parentId)
     {
         $data = array();
         $this->db->distinct();
         $this->db->select('title, page_id, parent_id, segment');
         $this->db->order_by("created");
	     $this->db->where("activate", 1);
         $this->db->where("parent_id", $parentId);
	     $Q = $this->db->get('page');
	     if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[] = $row;
	       }
	    }
	    $Q->free_result();  
	    return $data;
     }
     
     function getActiveSubPagesById($id){
	     $data = array();
         $this->db->order_by("created");
	     $this->db->where("activate", 1);
         $this->db->where("isshow_in_menu", 1);
	     $this->db->where('parent_id',id_clean($id));
	     $Q = $this->db->get('page');
	     if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[] = $row;
	       }
	    }
	    $Q->free_result();  
	    return $data; 
	 }
     
     function getLatestArticles($limit)
     {
         $data = array();
         $this->db->order_by("created", "DESC");
	     $this->db->where("activate", 1);
         $this->db->limit($limit);
	     $query = $this->db->get('page');
         return $query->result();
     }
     
     function addEditPage($page_id = 0){
		$data = array( 
			'title' => db_clean($_POST['title']),
            'titlekh' =>db_clean(isset($_POST['titlekh']) ? $_POST['titlekh']: ''),
            'category_id' => id_clean($_POST['category_id']),
            'segment' => db_clean($_POST['segment']),
            'keyword' => $_POST['keyword'],
            'keywordkh' =>isset($_POST['keywordkh']) ? $_POST['keywordkh']: '',
            'description' => $_POST['description'],
            'descriptionkh' =>isset($_POST['descriptionkh']) ? $_POST['descriptionkh']: '',
            'isshow_in_menu' => db_clean(isset($_POST['isshow_in_menu']) ? 1 : 0),
            'parent_id' => id_clean($_POST['parent_id']),
            'htmlcontent' => $_POST['htmlcontent'],
            'htmlcontentkh' =>isset($_POST['htmlcontentkh']) ? $_POST['htmlcontentkh']: '',
			'activate' => db_clean(isset($_POST['activate']) ? 1 : 0)
		);
        if($page_id > 0)
        {
            $this->db->set('modified', 'NOW()', FALSE);
            $this->db->where('page_id', id_clean($page_id));
    		$this->db->update('page', $data);   
        }
        else
        {
            $this->db->set('created', 'NOW()', FALSE);
    		$this->db->insert('page', $data);
            $lastId = $this->db->insert_id();
            if($_SESSION['role_id'] > 1)
            {
                $pageSettings = array();
                $maxId = $this->getMaxIdPageSetting();
                for($i = 1; $i <= (int)$_SESSION['role_id']; $i++)
                {
                    $newId = (int)($maxId + $i);
                    $pageSettings[] = array('id'=>$newId, 'page_id'=>$lastId, 'role_id'=>$i);
                }
                $this->addPageSettings($pageSettings);   
            }
            else
            {
                $this->addPageSetting($lastId);
            }
        }	
	 }
     
     function addPageSetting($pageId)
     {
        $maxId = $this->getMaxIdPageSetting();
    	$data = array( 
    		'id' => (int)($maxId+1),
            'page_id' => id_clean($pageId),
            'role_id' => id_clean($_SESSION['role_id'])
        );
        $this->db->insert('pagesetting', $data);
     }
     
     function addPageSettings($pages)
     {
        $this->db->insert_batch('pagesetting', $pages);
     }
     
     function updatePageSettings()
     {
        if($_SESSION['role_id'] == 1)
        {
            $roles = isset($_POST['role2']) ? $_POST['role2']: null;
            if(count($roles))
            { 
                $query = '(`role_id`=2);';
                $this->updatePageSettingByQuery($query);
                $this->updatePageSettingCondition($roles);
            } 
            
            $roles = isset($_POST['role3']) ? $_POST['role3']: null;
            if(count($roles))
            { 
                $query = '(`role_id`=3);';
                $this->updatePageSettingByQuery($query);
                $this->updatePageSettingCondition($roles);
            } 
        }
        elseif($_SESSION['role_id'] == 2)
        {
            $roles = isset($_POST['role3']) ? $_POST['role3']: null;
            if(count($roles))
            { 
                $query = '(`role_id`=3);';
                $this->updatePageSettingByQuery($query);
                $this->updatePageSettingCondition($roles);
            } 
        }
     }
     
     function updatePageSettingCondition($roles)
     {
        $maxId = $this->getMaxIdPageSetting();
        for($i = 0; $i < count($roles); $i++)
        {
            $settings = explode(',', $roles[$i]);
            if(count($this->getPageSettingByPageIdRoleId($settings[0], 0)))
            {
                $data = array('page_id'=>$settings[0], 'role_id'=>$settings[1]);
                $this->db->where('page_id',id_clean($settings[0]));
                $this->db->where('role_id',0);
                $this->db->update('pagesetting', $data);      
            }
            else
            {
                $newId = (int)($maxId+(int)($i+1));
                $data = array('id'=>$newId, 'page_id'=>$settings[0], 'role_id'=>$settings[1]);
                $this->db->insert('pagesetting', $data);
            }
        }
     }
     
     function updatePageSettingByQuery($query)
     {
        $this->db->set('role_id',0);
        $this->db->where($query);
        $this->db->update('pagesetting');
     }
     
     function getPageSettingByPageIdRoleId($pageId, $roleId)
     {
        $data = array();
	    $this->db->where('page_id',id_clean($pageId));
        $this->db->where('role_id',0);
	    $this->db->limit(1);
	    $Q = $this->db->get('pagesetting');
	    if ($Q->num_rows() > 0){
	      $data = $Q->row_array();
	    }
	
	    $Q->free_result();    
	    return $data;    
     }
     
     function getMaxIdPageSetting()
     {
        $query = $this->db->query('SELECT max(id) as max_id FROM pagesetting'); 
        $row = $query->row_array();
        $max_id = $row['max_id'];  
        return $max_id != null ? $max_id : 0;
     }
	 
	 function deletePage($id){
	 	$this->db->where('page_id', id_clean($id));
		$this->db->delete('page');	
	 }
}
?>